﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.IO;

public partial class admin_allergy_info_list : AdminBasePage
{
    public int isaudit = 0;
    public static readonly string ArticleVideoLayerids = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleVideoLayerids");
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        {
            //this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params["layerid"]);
            Grid_viewBild();
        }
    }

    private void Grid_viewBild()
    {

        int columnid = int.Parse(Request.Params["columnid"]);
        ColumnModel cm = new ColumnBll().GetColumn(columnid);
        isaudit = cm.Isauditing;

        //this.audittd.Visible = false;
        //this.pubtd.Visible = false;
        CmsArticleQuery cq = new CmsArticleQuery();
        cq.columnid = columnid;
        CmsArticleBll cabll = new CmsArticleBll();
        int pagesize = 10;

        //if (Request.Params["qdocstate"] != null && Request.Params["qdocstate"] != "")
        //{
        //    cq.docstate = Request.Params["qdocstate"];
        //    if (cq.docstate == "0")
        //    {


        //        if (isaudit == 1)
        //        {
        //            //this.audittd.Visible = true;
        //        }
        //        else
        //        {
        //            this.pubtd.Visible = true;
        //        }

        //    }
        //    else if (cq.docstate == "2")
        //    {
        //        this.pubtd.Visible = true;
        //    }


        //}

        if (Request.Params["rows"] != null && Request.Params["rows"] != "")
        {
            pagesize = int.Parse(Request.Params["rows"]);
            this.rows.Text = pagesize.ToString();
        }
        if (Request.Params["ordername"] != null && Request.Params["ordername"] != "")
        {
            cq.ordername = Request.Params["ordername"];

        }
        this.ordername.Text = cq.ordername;
        if (Request.Params["ordertype"] != null && Request.Params["ordertype"] != "")
        {
            cq.ordertype = Request.Params["ordertype"];

        }
        this.ordertype.Text = cq.ordertype;
        if (Request.Params["querykey"] != null && Request.Params["querykey"] != "")
        {
            cq.querykey = Request.Params["querykey"];
            this.querykey.Text = cq.querykey;
        }
        if (Request.Params["queryvalue"] != null && Request.Params["queryvalue"] != "")
        {
            cq.queryvalue = Request.Params["queryvalue"];
            this.queryvalue.Text = cq.queryvalue;
        }
        if (Request.Params["querytype"] != null && Request.Params["querytype"] != "")
        {
            cq.querytype = Request.Params["querytype"];
            this.querytype.Text = cq.querytype;
        }

        if (Request.Params["pageNum"] != null && Request.Params["pageNum"] != "")
        {
            try
            {
                GridView1.PageIndex = int.Parse(Request.Params["pageNum"]) - 1;
            }
            catch { }
        }
        IList datalist = cabll.getCmsArticleList(cq);

        //if (datalist.Count < 1)
        //{
        //    this.deltd.Visible = false;
        //    this.pubtd.Visible = false;
        //    this.audittd.Visible = false;
        //}
        //else
        //{
        //    this.deltd.Visible = true;
        //    this.pubtd.Visible = true;
        //    this.audittd.Visible = true;
        //}

        GridView1.DataSource = datalist;
        GridView1.PageSize = pagesize;
        GridView1.DataBind();
        this.pagehtml.Text = getPageHtml(GridView1.PageCount, GridView1.PageIndex + 1, datalist.Count);
    }

    protected void Add_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], ADD);
        Response.Redirect("info_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Del_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], DEL);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().DelCmsArticle(ids);
        if (r)
        {
            this.insertLog("批量删除文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Delone_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], DEL);
        string aid = ((LinkButton)sender).CommandArgument;
        DataTable dt = BLLArticleInfo.selArticle(aid);
        foreach (DataRow row in dt.Rows)
        {
            string toppic = row["toppic"].ToString();
            string hotpic = row["hotpic"].ToString();
            string pic = row["pic"].ToString();
            if (!string.IsNullOrEmpty(toppic) || toppic != "")
            {
                File.Delete(Server.MapPath("../../allergy/uploadfiles/image/") + toppic);
            }
            if (!string.IsNullOrEmpty(hotpic) || hotpic != "")
            {
                File.Delete(Server.MapPath("../../allergy/uploadfiles/image/") + hotpic);
            }
            if (!string.IsNullOrEmpty(pic) || pic != "")
            {
                File.Delete(Server.MapPath("../../allergy/uploadfiles/image/") + pic);
            }
        }

        bool r = new CmsArticleBll().DelCmsArticle(aid);
        if (r)
        {
            insertLog("删除文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }

    protected void Pub_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], PUB);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().Pubs(ids, GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("批量发布文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Pubone_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], PUB);

        string aid = ((LinkButton)sender).CommandArgument;
        bool r = new CmsArticleBll().Pubs(aid, GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("发布文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }
    //撤消发布
    protected void RePubone_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], PUB);

        string aid = ((LinkButton)sender).CommandArgument;
        bool r = new CmsArticleBll().RePubOne(int.Parse(aid), GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("撤消发布文章:" + new CmsArticleBll().GetCmsArticleModel(int.Parse(aid)).Title);
        }
        Grid_viewBild();
    }

    protected void Mod_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], MOD);
        Response.Redirect("info_detail.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&id=" + ((LinkButton)sender).CommandArgument, true);
    }
    protected void Seo_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], MOD);
        Response.Redirect("info_seo.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"] + "&id=" + ((LinkButton)sender).CommandArgument, true);
    }
    protected void Audit_click(object sender, EventArgs e)
    {
        //this.PopedomValidate(Request.Params["layerid"], AUDIT);
        string ids = Request.Params["ids"];
        bool r = new CmsArticleBll().Audits(ids, "2", GetSessionUsersModel().Username);
        if (r)
        {
            insertLog("批量审核文章:" + ids);
        }
        Grid_viewBild();
    }

    protected void Preview_click(object sender, EventArgs e)
    {
        string colmid = Request.Params["columnid"].ToString();
        string aid = ((LinkButton)sender).CommandArgument;
        DataTable dt = BLLArticleInfo.getColumnName(colmid);
        string ename = null;
        foreach (DataRow row in dt.Rows)
        {
            ename = row["ECLASSNAME"].ToString();
        }

        DataTable dtc = BLLArticleInfo.GetArticleContent(colmid, aid);
        string staticname = null;
        foreach (DataRow row in dtc.Rows)
        {
            staticname = row["STATICPAGENAME"].ToString();
            if (staticname == null || staticname == "")
            {
                staticname = row["ARTICLEID"].ToString() + ".html";
            }
        }

        string classid = null;
        if (colmid == "196" || colmid == "197" || colmid == "198" || colmid == "199" || colmid == "200" || colmid == "201")
        {
            classid = "196";
        }
        else if (colmid == "204" || colmid == "207" || colmid == "208" || colmid == "209" || colmid == "210")
        {
            classid = "204";
        }
        else if (colmid == "180")
        {
            classid = "180";
        }

        else if (colmid == "215")
        {
            classid = "215";
        }

        string path = null;
        if (classid == "196" || classid == "180")
        {
            path = "/allergy/lore/article_content.aspx?classId=" + classid + "&columnId=" + colmid + "&artId=" + aid;
        }
        else if (classid == "204")
        {
            path = "/allergy/plan/plan_content.aspx?classId=" + classid + "&columnId=" + colmid + "&artId=" + aid;
        }
        else if (classid == "215")
        {
            path = "/allergy/doctor/study.aspx?aid=" + aid;

        }
        // ClientScript.RegisterStartupScript(this.GetType(), "infopreview", "<script type='text/javascript'>window.open('info_preview.aspx?cid=" + Request.Params["columnid"] + "&aid=" + ((LinkButton)sender).CommandArgument + "','_blank');</script>");

        //ClientScript.RegisterStartupScript(this.GetType(), "infopreview", "<script type='text/javascript'>window.open('"+path + staticname+"','_blank');</script>");

        ClientScript.RegisterStartupScript(this.GetType(), "infopreview", "<script type='text/javascript'>window.open('" + path + "','_blank');</script>");

    }
    //public string classid;
    //public string url;
    //public string path;
    //public string ename;
    //public string parentname;
    //public string childname;
    //public string staticname;
    //public string arttitle;
    //public string artkeywords;
    //public string artdesc;
    //public string coltitle;
    //public string colkeywords;
    //public string coldesc;

    public string colmid;
    protected void static_click(object sender, EventArgs e)
    {
        try
        {
            string colmid = Request.Params["columnid"].ToString();
            string aid = ((LinkButton)sender).CommandArgument;
            DataTable dt = BLLArticleInfo.getColumnName(colmid);
            DataTable dtc = BLLArticleInfo.GetArticleContent(colmid, aid);
            GenerateStaticPage(dt.Rows[0], dtc.Rows[0]);
        }
        catch (Exception ex)
        {
            Response.Write("生成文件时遇到错误。 错误信息：" + ex.Message);
            Response.End();
        }
    }

    private void GenerateStaticPage(DataRow columnRow, DataRow articleRow)
    {
        try
        {
            if (columnRow == null) throw new ArgumentNullException("columnRow");
            if (articleRow == null) throw new ArgumentNullException("articleRow");

            string coltitle = columnRow["Columntitle"].ToString();
            string colkeywords = columnRow["ColumnKeyword"].ToString();
            string coldesc = columnRow["ColumnDesc"].ToString();

            string aid = articleRow["ARTICLEID"].ToString();
            string staticname = articleRow["STATICPAGENAME"].ToString();
            string arttitle = articleRow["ARTICLETITLE"].ToString();
            string artkeywords = articleRow["ARTICLEKEYWORD"].ToString();
            string artdesc = articleRow["ARTICLEDESC"].ToString();
            string parentname = articleRow["parentename"].ToString();
            string childname = articleRow["childenamel"].ToString();

            if (string.IsNullOrEmpty(staticname))
                staticname = articleRow["ARTICLEID"].ToString() + ".html";

            if (string.IsNullOrEmpty(arttitle))
                arttitle = coltitle;

            if (string.IsNullOrEmpty(artkeywords))
                artkeywords = colkeywords;

            if (string.IsNullOrEmpty(artdesc))
                artdesc = coldesc;

            BLLArticleInfo.updstaticname(staticname, arttitle, artkeywords, artdesc, aid);

            string classid = null;
            if (colmid == "196" || colmid == "197" || colmid == "198" || colmid == "199" || colmid == "200" || colmid == "201")
            {
                classid = "196";
            }
            else if (colmid == "204" || colmid == "207" || colmid == "208" || colmid == "209" || colmid == "210")
            {
                classid = "204";
            }
            else if (colmid == "180")
            {
                classid = "180";
            }
            else if (colmid == "215")
            {
                classid = "215";
            }

            string url = null;
            if (classid == "196")
            {
                url = "http://www.yufangguomin.com/allergy/lore/article_content.aspx?columnId=" + colmid + "&artId=" + aid + "&classId=" + classid;
                //path = HttpContext.Current.Server.MapPath("../../allergy/lore/" + ename + "/");

            }
            else if (classid == "204")
            {
                url = "http://www.yufangguomin.com/allergy/plan/plan_content.aspx?columnId=" + colmid + "&artId=" + aid + "&classId=" + classid;
                //path = HttpContext.Current.Server.MapPath("../../allergy/plan/" + ename + "/");

            }
            else if (classid == "180")
            {
                url = "http://www.yufangguomin.com/allergy/lore/article_content.aspx?columnId=" + colmid + "&artId=" + aid + "&classId=" + classid;
                //path = HttpContext.Current.Server.MapPath("../../allergy/" + ename + "/");

            }
            else if (classid == "215")
            {
                url = "http://www.yufangguomin.com/allergy/doctor/study.aspx?&aid=" + aid;
                //path = HttpContext.Current.Server.MapPath("../../allergy/doctor/" + ename + "/");
            }

            string path = HttpContext.Current.Server.MapPath("../../allergy/" + parentname + "/" + childname + "/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (!string.IsNullOrEmpty(url))
                GenerateHTML.generateInfoHTML(url, path + staticname);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void allstatic_click(object sender, EventArgs e)
    {
        try
        {
            string colmid = Request.Params["columnid"].ToString();
            DataTable dt1 = BLLArticleInfo.getColumnName(colmid);
            DataTable dt2 = BLLArticleInfo.selectb(colmid);
            foreach (DataRow row in dt2.Rows)
            {
                GenerateStaticPage(dt1.Rows[0], row);
            }
        }
        catch (Exception ex)
        {
            Response.Write("生成文件时遇到错误。 错误信息：" + ex.Message);
            Response.End();
        }
    }

}
